var vm = new Vue({
    el: '#hl-body',
    data: function () {
        return {
            accountDetail:{},
            templateList:[],
            templateId:'',
            cloudData:[],
            hyperVData:[
                {url:""}
            ],
            navBarList:[],
            defaultNar:[
                {
                    href:'/default/images/default_banner.jpg',
                    name:'default_banner.jpg',
                    status:'success',
                    url: '/default/images/default_banner.jpg'
                }
            ],
            vHostProductHotList:[
                {price:""}
            ],
            vDiyProductHotList: [
                {price: ''}
            ],
            hyperCfg : {
                zone: {},
                series: {},
                model: {},
                spec: {},
                disk: {},
                memory: {}
            },
            cacheData: {},
            /**
             * 热门top4.
             */
            domainProductHotList: [],
            /**
             * 活动top4.
             */
            domainProductActiveList: [],
            /**
             * 新上top4.
             */
            domainProductNewList: []
        }
    },
    methods: {
        /**
         * 加载产品.
         */
        loadConfig: function () {
            // $("#section1").css('background-image',"url('/default/images/index_23.jpg')");
            var self = this;
            var service = {code: 'cloudPromotion'};
            var fn="getparam";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);
            Commonjs.ajaxSilence(sysurl, params, false, function (data) {
                if (data.data.length > 0) {
                    self.cloudData = data.data[0].paramValue;
                    self.hyperVData = self.cloudData.hyperV.data;
                    $.each(self.hyperVData, function (key, item) {
                        self.hyperVData[key].remarks = item.remark;
                        self.queryPrice(self.hyperVData[key]);

                        var preselection = JSON.stringify(self.hyperVData[key]);
                        self.hyperVData[key].url = "/default/cloud/cloudservice.html?preselection="+encodeURIComponent(preselection);
                    });
                    var defChoose = false;
                }
            }, null, true);
        },

        /**
         * 获取价格参数.
         * @param cpu
         * @param memory
         * @returns {*}
         */
        specCfg: function(cpu, memory) {
            var self = this;
            var key = cpu + '_' + memory;
            if (self.cacheData[key]) {
                return self.cacheData[key];
            }

            var getSpecCfg = function () {
                var cfg = {};

                //regionId
                var hostData = [];
                if (!self.hyperCfg.regionList) {
                    var service = {};
                    service.hostType = "hyperv";
                    var fn = "queryHostlist";
                    service = Commonjs.jsonToString(service);
                    var params = Commonjs.getParams(fn, service);
                    hostData = Commonjs.ajax(weburl, params, false);

                    self.hyperCfg.regionList = hostData;
                } else {
                    hostData = self.hyperCfg.regionList;
                }


                for (var iRegion = 0; iRegion < hostData.data.length; iRegion++)  {
                    cfg.regionId = hostData.data[iRegion].regionId;


                    //modelId
                    var zoneData = [];
                    if (self.hyperCfg.zone[cfg.regionId]) {
                        zoneData = self.hyperCfg.zone[cfg.regionId];
                    } else {
                        service = {};
                        service.regionId = cfg.regionId;
                        fn = "queryDicZoneList";
                        service = Commonjs.jsonToString(service);
                        params = Commonjs.getParams(fn, service);//获取参数
                        zoneData = Commonjs.ajax(weburl, params, false);
                        if (!zoneData.data || hostData.data.length < 1) {
                            continue;
                        }
                        self.hyperCfg.zone[cfg.regionId] = zoneData;
                    }

                    for (var iZone = 0; iZone<zoneData.data.length; iZone++)  {
                        cfg.zoneId = zoneData.data[iZone].zoneId;

                        //系列
                        var seriesData = [];
                        if (!self.hyperCfg.series[cfg.zoneId]) {
                            service = {};
                            service.zoneId = cfg.zoneId;
                            fn = "queryZoneInSeriesList";
                            service = Commonjs.jsonToString(service);
                            params = Commonjs.getParams(fn, service);//获取参数
                            seriesData = Commonjs.ajax(weburl, params, false);
                            if (!seriesData.data || seriesData.data.length < 1) {
                                continue;
                            }
                            self.hyperCfg.series[cfg.zoneId] = seriesData;
                        } else {
                            seriesData = self.hyperCfg.series[cfg.zoneId];
                        }


                        for (var iSeries = 0; iSeries < seriesData.data.length; iSeries++)  {
                            cfg.seriesId = seriesData.data[iSeries].seriesId;

                            //机型
                            var modelData = [];
                            if (self.hyperCfg.model[cfg.seriesId]) {
                                modelData = self.hyperCfg.model[cfg.seriesId];
                            } else {
                                service = {};
                                service.hostType = "hyperv";
                                service.seriesId = cfg.seriesId;
                                fn = "getDicModelInSeries";
                                service = Commonjs.jsonToString(service);
                                params = Commonjs.getParams(fn, service);//获取参数
                                modelData = Commonjs.ajax(weburl, params, false);
                                if (!modelData.data || modelData.data.length < 1) {
                                    continue;
                                }
                                self.hyperCfg.model[cfg.seriesId] = modelData;
                            }

                            for(var iModel = 0; iModel < modelData.data.length; iModel++) {
                                cfg.modelId = modelData.data[iModel].modelId;

                                //instance type.
                                var specData = [];
                                if (self.hyperCfg.spec[cfg.modelId]) {
                                    specData = self.hyperCfg.spec[cfg.modelId];
                                } else {
                                    service = {};
                                    service.hostType = "hyperv";
                                    service.modelId = cfg.modelId;
                                    fn="getDicSpecCPU";
                                    service = Commonjs.jsonToString(service);
                                    params = Commonjs.getParams(fn,service);//获取参数数
                                    specData = Commonjs.ajax(weburl, params, false);
                                    if (!specData.data || specData.data.length < 1) {
                                        continue;
                                    }
                                    self.hyperCfg.spec[cfg.modelId] = specData;
                                }


                                for (var iSpec = 0; iSpec < specData.data.length; iSpec++)  {
                                    cfg.cpu = specData.data[iSpec].cpu;
                                    var keyCpu = cfg.modelId + '_c_p_' + cpu;

                                    var memoryData = []
                                    if (self.hyperCfg.memory[keyCpu]) {
                                        memoryData = self.hyperCfg.memory[keyCpu];
                                    } else {
                                        service = {};
                                        service.hostType = "hyperv";
                                        service.modelId = cfg.modelId;
                                        service.cpu = cfg.cpu;
                                        fn = "getDicSpecMemory";
                                        service = Commonjs.jsonToString(service);
                                        params = Commonjs.getParams(fn, service);//获取参数
                                        var memoryData = Commonjs.ajax(weburl, params, false);
                                        if (!memoryData.data || memoryData.data.length < 1) {
                                            continue;
                                        }
                                        self.hyperCfg.memory[keyCpu] = memoryData;
                                    }

                                    for (var iMemory = 0; iMemory < memoryData.data.length; iMemory ++) {
                                        var spec = memoryData.data[iMemory];
                                        if (parseInt(spec.cpu) === parseInt(cpu) && parseInt(spec.memory) === parseInt(memory)) {
                                            cfg.specCode = spec.specCode;
                                            return cfg;
                                        }
                                    }

                                }
                            }
                        }
                    }
                }

                return cfg;
            };

            var getDiskType = function (modelId) {
                var diskData = [];

                if (self.hyperCfg.disk[modelId]) {
                    diskData = self.hyperCfg.disk[modelId];
                } else {
                    var service = {};
                    service.modelId = modelId;
                    var fn="queryDicModelDiskList";
                    service = Commonjs.jsonToString(service);
                    var params = Commonjs.getParams(fn,service);
                    diskData = Commonjs.ajax(weburl, params, false);
                    if (!diskData.data || diskData.data.length < 1) {
                        return '';
                    }
                    self.hyperCfg.disk[modelId] = diskData;
                }

                return diskData.data[0].typeId;

            };

            var nowCfg = getSpecCfg();
            if (nowCfg.specCode) {
                nowCfg.diskType = getDiskType(nowCfg.modelId);
                self.cacheData[key] = nowCfg;
                return nowCfg;
            }

            return {};
        },
        queryPrice: function (data) {
            var specCfg = this.specCfg(data.cpu, data.memory);
            data.regionId = specCfg.regionId;
            data.zoneId = specCfg.zoneId;
            data.seriesId = specCfg.seriesId;
            data.modelId = specCfg.modelId;
            data.diskType = specCfg.diskType;
            if (!specCfg.diskType) {
                data.price = '';
                return;
            }
            var service = {
                "hostType":"hyperv",
                "productCode":"10008",
                "regionId": specCfg.regionId,
                "modelId":specCfg.modelId,
                "cpu": data.cpu,
                "memory": data.memory,
                "bandwidth": data.bandwidth,
                "osType":"linux",
                "systemDiskSize":data.sysDataSize,
                "systemDiskType":specCfg.diskType,
                "netty":"PRI",
                "ipType":"PRI",
                "chosenPublicIp":1,
                "dataDisklist":"[{\"dataDiskType\":\""+specCfg.diskType+"\",\"dataDiskSize\":\""+(data.dataSize ? data.dataSize : 0)+"\"}]",
                "applyTime":"1",
                "priceType":"m",
                "num":"1"
            };
            var fn="getPrice";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);
            Commonjs.ajaxSilence(weburl, params, false, function (result) {
                if (result.data) {
                    data.price = result.data.payPrice;
                }
            });
        },

        loadVhostProduct: function () {
            var self = this;
            var service = {};
            service.productClass = 'vhost';
            service.page = 1;
            service.pageSize = 3;
            service.recommend = 1;
            var fn="getCustomerProductList";
            service = Commonjs.jsonToString(service)
            var params = Commonjs.getParams(fn,service);
            Commonjs.ajaxSilence(weburl, params, false, function (data) {
                if (data.data.length > 0) {
                    self.vHostProductHotList = data.data;
                    $.each(self.vHostProductHotList, function (key, item) {
                        if(key == 0){
                            self.vHostProductHotList[key].price = 160;
                        }else if(key == 1){
                            self.vHostProductHotList[key].price = 200;
                        }else{
                            self.vHostProductHotList[key].price = 180;
                        }
                        self.vHostProductHotList[key].productDetail = item.productDetail.replace(/ /g,"&nbsp;");
                        // self.hyperVData[key].url = "default/cloud/cloudservice.html?cpu="+item.cpu+"&memory="+item.memory+"&sysDataSize="+item.sysDataSize+"&sysDataType="+item.sysDataType;
                    });
                }
            }, null, true);

        },
        loadDiyProduct: function() {
            var self = this;
            var service = {};
            service.productClass = 'diy';
            service.page = 1;
            service.pageSize = 3;
            service.recommend = 1;
            var fn="getCustomerProductList";
            service = Commonjs.jsonToString(service)
            var params = Commonjs.getParams(fn,service);
            Commonjs.ajaxSilence(weburl, params, false, function (data) {
                if (data.data.length > 0) {
                    self.vDiyProductHotList = data.data;
                    $.each(self.vDiyProductHotList, function (key, item) {
                        if (item.productParam.payPrice1 > 0) {
                            self.vDiyProductHotList[key].price = item.productParam.payPrice1;
                            self.vDiyProductHotList[key].priceUnit = '元/月';
                        } else if (item.productParam.payPrice3 > 0) {
                            self.vDiyProductHotList[key].price = item.productParam.payPrice3;
                            self.vDiyProductHotList[key].priceUnit = '元/季度';
                        } else if (item.productParam.payPrice6 > 0) {
                            self.vDiyProductHotList[key].price = item.productParam.payPrice6;
                            self.vDiyProductHotList[key].priceUnit = '元/半年';
                        } else if (item.productParam.payPrice12 > 0) {
                            self.vDiyProductHotList[key].price = item.productParam.payPrice12;
                            self.vDiyProductHotList[key].priceUnit = '元/年';
                        }
                        self.vDiyProductHotList[key].productDetail = item.productDetail.replace(/ /g,"&nbsp;");
                    });
                }
            }, null, true);
        },
        loadNavScrollBar:function(){
            var self = this;
            var service = {code: 'navBarImg'};
            var fn="getparam";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);
            Commonjs.ajaxSilence(sysurl, params, false, function (data) {
                if(data.data.length > 0){
                    self.navBarList = data.data[0].paramValue;
                }else{
                    self.navBarList = self.defaultNar;
                }
                
                var htmls = "";
                self.navBarList.forEach(function (item,index) {
                    if (!Commonjs.validateImage(item.url)) {
                        var url = '/default/images/index_' + (index + 23) + '.jpg';
                        item.url = url;
                    }
                    htmls +='<div class="section" id="section'+index+'" style="cursor: pointer;" onclick="location.href=\''+item.hrefs+'\'"></div>';
                });
                $(".sections").html(htmls);
                
            }, null, true);
        },
        /**
         * 加载产品.
         */
        loadDomainProduct: function () {
            var self = this;
            var service = {};
            var fn="queryDomainPackage";
            service = Commonjs.jsonToString(service)
            var params = Commonjs.getParams(fn,service);
            winiisRequest.get(weburl, params, function (data) {
                self.domainProductHotList = data.data.domainProductHotList;
                self.domainProductActiveList = data.data.domainProductActiveList;
                self.domainProductNewList = data.data.domainProductNewList;
            });
        }

    },
    mounted: function () {
        this.loadConfig();
        this.loadNavScrollBar();
        this.loadVhostProduct();
        this.loadDiyProduct();
        this.loadDomainProduct();
    }
});



// JavaScript Document
$(function (){
    queryNotice();
    getProDefaultPrice();
    querDicSuffix();
    $("#current_ext").click(function(){
        $("#domainext").show();
    });
    vm.navBarList.forEach(function (item,index) {
        $("#section"+index).css('background-image','url('+item.url+')');
    });
    // $("#section1").css('color',"#999");
    // $("#section1").css('background-image',"url('/default/images/index_23.jpg')");
    document.onclick = function (event)
    {
        var e = event || window.event;
        var elem = e.srcElement||e.target;
        while(elem)
        {
            if(elem.id == "current_ext")
            {
                return;
            }
            elem = elem.parentNode;
        }
        //隐藏div的方法
        $("#domainext").hide();
    }

    jQuery("#lanmu-menu").slide( {
        type : "menu",
        titCell : ".yzj-box", //鼠标触发对象
        targetCell : ".sub",
        effect : "slideDown",
        delayTime : 150, //效果时间
        triggerTime : 0, //鼠标延迟触发时间（默认150）
        returnDefault : true//鼠标移走后返回默认状态，
    });

    jQuery("#slideBox").slide({
        mainCell : ".bd ul",
        effect : "fold",
        autoPlay : true,
        delayTime : 1000
    });
    jQuery(".txtMarquee-left").slide({mainCell:".bd ul",autoPlay:true,effect:"leftMarquee",vis:5,interTime:50});
});

function getProDefaultPrice(){
    var service = {};
    service.productclass = "domain";
    var fn="getProDefaultPrice";
    service = Commonjs.jsonToString(service)
    var params = Commonjs.getParams(fn,service);
    Commonjs.ajaxTrue(weburl,params,showdata);
}

function showdata(data){
    var doaminlist='';
    $('#doaminlist').html("");
    if(data.result="success"){
        if(data.data.length>0){
            BaseForeach(data.data, function (i, item) {
                if (item.recommend == 1) {
                    doaminlist+="<tr>";
                    doaminlist+="<td>"+item.productName+"</td>";
                    doaminlist+="<td>"+item.productDetail+"</td>";
                    doaminlist+="<td>"+item.buyPrice+"</td>";
                    doaminlist+="<td>"+item.renewPrice+"</td>";
                    doaminlist+="</tr>";
                }
            });
            $('#doaminlist').html(doaminlist);
        }
    }
}

//获取公告
function queryNotice(){
    var service = {};
    service.parentId = "notice";
    var fn="querySysArticleList";
    service = Commonjs.jsonToString(service);
    var params = Commonjs.getParams(fn,service);//获取参数
    $.ajax({
        datatype:"json",
        type:"POST",
        url: weburl,
        data:params,
        async: true,
        timeout : 8000,
        cache : false,
        success: function(obj){
            data =jQuery.parseJSON(obj);
            $("#notice").empty();
            var notice='';
            if (data.data.length>0){
                var tpl = Commonjs.getCfgVal(configParam.common.cfgKey.template);
                BaseForeach(data.data,function(i,item){
                    if(i<3){
                        notice+='<li>['+jsonDateFormat(item.publishTime)+']<a href="'+tpl+'notice/noticeinfo.html?id='+item.id+'">'+item.title+'</a></li>';
                    }
                });
            }
            $('#notice').html(notice);
        }
    });
};

//查询域名
function subQuery(){
    var domainName = $('#domainName').val();
    $('#domainName').val("");
    if(domainName.indexOf(".")>=0){
        domainName = domainName.substring(0,domainName.indexOf("."));
    }

    if(Commonjs.isEmpty(domainName)){
        Commonjs.alert("请输入查询的域名");
        return false;
    }
    func=$("#current_ext").html();
    domainName = escape($.trim((domainName)));
    func = escape(func);
    location.href = Commonjs.getCfgVal(configParam.common.cfgKey.template) + "domain/domainlist.html?domain="+domainName+"&suffix="+func;
}

//获取域名后缀
function querDicSuffix(){
    var service = {};
    service.page=1;
    service.pageSize=200;
    var fn="querDicSuffix";
    service = Commonjs.jsonToString(service)
    var params = Commonjs.getParams(fn,service);//获取参数
    var suffix="";

    $.ajax({
        datatype:"json",
        type:"POST",
        url: weburl,
        data:params,
        async: true,
        timeout : 8000,
        cache : false,
        success: function(obj){
            data =jQuery.parseJSON(obj);
            $('#domainext').html("");
            var itemlist=data.data.getUserDomainByUserName;
            suffix+='<ul style="width:100px;overflow-y:scroll;overflow-x:hidden;">'
            if(itemlist.length>0){
                BaseForeach(itemlist,function(i,item){
                    suffix+='<li>'+item.name+'</li>';
                });
            }
            suffix+='</ul>';
            $('#domainext').html(suffix);
            $("#domainext li").on('click',function(){
                var ext=$(this).html();
                $("#current_ext").html("");
                $("#current_ext").html(ext);
            });
        }
    });
}